package com.gc.ip.demo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.MaskFormatter;
import javax.swing.text.NavigationFilter;
import javax.swing.text.Position;

@SuppressWarnings("serial")
public class TestDemo extends JPanel
{
	private JTextField textField = null;

	public TestDemo()
	{
		initGUI();
	}

	private void initGUI()
	{

		MaskFormatter mf = null;
		try
		{
			mf = new MyMaskFormatter();
			mf.setMask("###.###.###.###");
			
			mf.setMask("HHHH:HHHH:HHHH:HHHH:HHHH:HHHH:HHHH:HHHH");
			mf.setPlaceholderCharacter('0');
		}
		catch(ParseException e)
		{
			e.printStackTrace();
		}
		textField = new JFormattedTextField(mf);
		Font font = new Font("Courier New", Font.PLAIN, 12);
		textField.setFont(font);
        
		this.setLayout(new BorderLayout());
		this.add(textField, BorderLayout.CENTER);
	}
	
	class MyDocumentListener implements DocumentListener
	{
		private JTextField textField = null;
		public MyDocumentListener(JTextField textField)
		{
			this.textField = textField;
		}
		/**
	     * Gives notification that there was an insert into the document.  The 
	     * range given by the DocumentEvent bounds the freshly inserted region.
	     *
	     * @param e the document event
	     */
	    public void insertUpdate(DocumentEvent e)
	    {
	    	textField.setCaretPosition(textField.getCaretPosition() + 3);
	    }

	    /**
	     * Gives notification that a portion of the document has been 
	     * removed.  The range is given in terms of what the view last
	     * saw (that is, before updating sticky positions).
	     *
	     * @param e the document event
	     */
	    public void removeUpdate(DocumentEvent e)
	    {
	    	
	    }

	    /**
	     * Gives notification that an attribute or set of attributes changed.
	     *
	     * @param e the document event
	     */
	    public void changedUpdate(DocumentEvent e)
	    {
	    	
	    }
	}
	class MyMaskFormatter extends MaskFormatter
	{
		private NavigationFilter navFilter = null;
		
		protected NavigationFilter getNavigationFilter() {
	        if (navFilter == null) {
	        	navFilter = new MyNavigationFilter();
	        }
	        return navFilter;
	    }
		private class MyNavigationFilter extends NavigationFilter
		{
			public void setDot(FilterBypass fb, int dot, Position.Bias bias) 
			{
				String text = getFormattedTextField().getText();
				System.out.printf("setDot: text=%s, dot=%d, bias=%s\n", text, dot, bias);

		        super.setDot(fb, dot, bias);
		    }

		    public void moveDot(FilterBypass fb, int dot, Position.Bias bias) 
		    {
		    	String text = getFormattedTextField().getText();
		    	System.out.printf("moveDot: text=%s, dot=%d, bias=%s\n", text, dot, bias);
		        super.setDot(fb, dot, bias);
		    }
		    
			public int getNextVisualPositionFrom(JTextComponent text, int pos, Position.Bias bias,
				int direction, Position.Bias[] biasRet) throws BadLocationException
			{
				int ret = super.getNextVisualPositionFrom(text, pos, bias, direction, biasRet);
				
				String str = getFormattedTextField().getText();
		    	System.out.printf("getNextVisualPositionFrom: text=%s, ret=%d\n", str, ret);
		    	
		    	return ret;
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Demo");

		TestDemo tableDemo = new TestDemo();
		frame.getContentPane().add(tableDemo);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(300, 200));
		frame.pack();
		frame.setVisible(true);
	}

}
